// https://www.jianshu.com/p/9f7132967111
/**
 * 进度条实现。
 */
const log = require('single-line-log').stdout;
const strformat = require('str-format').format;
const clicolor = require('cli-color');

/**
 * 封装一个进度条工具。
 */
function ProgressBar(description, bar_length) {
	this.description = description || "PROGRESS";
	this.length = bar_length.length || 28;

	// 刷新进度条图案，文字的方法
	this.render = function(opts) {
		var percentage = (opts.completed / opts.total).toFixed(4);
		var cell_num = Math.floor(percentage * this.length);
		// 拼接黑色条
		var cell = '';
		for (var i = 0; i < cell_num; i++) {
			cell += '█';
		}
		// 拼接灰色条
		var empty = '';
		for (var i = 0; i < this.length - cell_num; i++) {
			empty += '░';
		}

		var percent = (100 * percentage).toFixed(2);
		/**
		 * 使用cli-color进行包装美化。
		 */
		this.description = clicolor.blue.bold(this.description);
		cell = clicolor.green.bgBlack.bold(cell);
		opts.completed = clicolor.yellow.bold(opts.completed);
		opts.total = clicolor.blue.bold(opts.total);
		opts.status = percent == 100.00 ? clicolor.green.bold(opts.successText) : clicolor.red.bold(opts.loadingText);


		// 拼接最终文本
		cmdtext = strformat("<{}:{}%> {}{}  [ {}/{} `{}`]", [this.description, percent,
			cell, empty, opts.completed, opts.total, opts.status
		]);
		log(cmdtext);
	};
}

/**
 * 模块导出。
 */
module.exports = ProgressBar;
